/**
 * <b>项目名：</b>亲子分享-服务端项目<br/>
 * <b>包名：</b>com.saas.controller.production<br/>
 * <b>文件名：</b>APIController.java<br/>
 * <b>版本信息：</b>V1.0<br/>
 * <b>日期：</b>2014-8-29-下午05:14:17<br/>
 * <b>作者：</b><br/>
 * <b>Copyright (c)</b> 2014坚果(北京)科技有限公司-版权所有<br/>
 */
package com.saas.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saas.util.ResponseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.saas.exception.ServiceException;
import com.saas.service.FileService;
import com.saas.util.Constant;

 /**
 *
 * <b>类名称：</b>FunctionsController<br/>
 * <b>类描述：</b>高级查询控制器<br/>
 * <b>创建人：</b><br/>
 * <b>修改人：</b><br/>
 * <b>修改时间：</b>2014-9-15 下午05:14:17<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 *
 */
@RestController
@RequestMapping(value = "/api")
public class FileController {
	
	protected static Logger logger = LoggerFactory.getLogger(FileController.class);
	
	@Autowired
	private FileService fileService;
	
	@RequestMapping(value = "/post/files", method=RequestMethod.POST)
	public @ResponseBody String post(
			@RequestBody String body,
			HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String documents = fileService.uploadFile( body);
			responseEntity.setData(documents);
			responseEntity.setMsg(Constant.API_CALL_STATUS_SUCCESS_MSG);
			responseEntity.setStatus(Constant.API_CALL_STATUS_SUCCESS);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			responseEntity.setMsg(e.getMessage());
			responseEntity.setStatus(Constant.API_CALL_STATUS_ERROR_EXCEPTION);
		}
		request.setAttribute(Constant.CLIENT_RESPONSE, responseEntity);
		return null;
	}
	
	/**
	 * get(查询文件)
	 * 
	 * @param id 要操作模型中的字段
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping(value = "/get/files/{id}", method=RequestMethod.GET)
	public @ResponseBody String get(
			@PathVariable String id,
			HttpServletRequest request, HttpServletResponse response) throws IOException{

		String returnValue = "";
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			returnValue = fileService.get(id);
			responseEntity.setData(returnValue);
			responseEntity.setMsg(Constant.API_CALL_STATUS_SUCCESS_MSG);
			responseEntity.setStatus(Constant.API_CALL_STATUS_SUCCESS);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			responseEntity.setData(returnValue);
			responseEntity.setMsg(e.getMessage());
			responseEntity.setStatus(Constant.API_CALL_STATUS_ERROR_EXCEPTION);
		}
		request.setAttribute(Constant.CLIENT_RESPONSE, responseEntity);
		return null;
	}
	
	/**
	 * 
	 * delete(删除文件)
	 * @param id 要操作模型中的字段
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	@RequestMapping(value = "/delete/files/{id}", method=RequestMethod.DELETE)
	public @ResponseBody String delete(
			@PathVariable String id, 
			HttpServletRequest request, HttpServletResponse response) throws IOException{
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			fileService.delete(id);
			
			responseEntity.setData(id);
			responseEntity.setMsg(Constant.API_CALL_STATUS_SUCCESS_MSG);
			responseEntity.setStatus(Constant.API_CALL_STATUS_SUCCESS);
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			responseEntity.setMsg(Constant.API_CALL_STATUS_ERROR_EXCEPTION_MSG);
			responseEntity.setStatus(Constant.API_CALL_STATUS_ERROR_EXCEPTION);
		}
		request.setAttribute(Constant.CLIENT_RESPONSE, responseEntity);
		return null;
	}
}
